
import axios from "axios";
import { getLogger, Logger } from "log4js";
import request from "request";
import { getAxios } from ".";

const qin = require('node-qiniu')
const logger = getLogger('qiniu')
export class qiniu {
    constructor(
        private access: string,
        private secret: string,
        private bucket: string
    ) {
        qin.config({
            access_key: this.access,
            secret_key: this.secret
        });
    }

    async upload(url: string, name: string) {
        try {
            const { status } = await axios.get(url)
        } catch (error) {
            return
        }
        console.log(url)
        try {
            const imagesBucket = qin.bucket(this.bucket);
            const puttingStream = imagesBucket.createPutStream(`${name}.png`);
            request.get(url).pipe(puttingStream)
            return `http://r4auhvjfb.hd-bkt.clouddn.com/${name}.png`
        } catch (error: any) {
            logger.error(`qiniu load ${error.message}`)
        }
        return `http://r4auhvjfb.hd-bkt.clouddn.com/error.png`
    }
}